package simbot.example.listener.event.message.group.request.image;

import catcode.CatCodeUtil;
import lombok.extern.slf4j.Slf4j;
import love.forte.simbot.annotation.Filter;
import love.forte.simbot.annotation.Filters;
import love.forte.simbot.annotation.OnGroup;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.sender.Sender;
import love.forte.simbot.filter.MatchType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import simbot.example.utils.pixiv.PixivUtil;
import simbot.example.utils.pixiv.dto.ImgDTO;
import simbot.example.utils.pixiv.dto.PixivDTO;

/**
 * 群信息-请求图片-监听
 *
 * @author minmin
 * @since 2021/06/26 0:22
 */
@Service
@Slf4j
public class GroupImageListen {
    @OnGroup
    @Filters(value = {
            @Filter(value = "涩图", matchType = MatchType.ENDS_WITH),
            @Filter(value = "色图", matchType = MatchType.ENDS_WITH)
    })
    public void GetResImg(GroupMsg groupMsg, Sender sender){
        PixivDTO randomHImage = null;
        ImgDTO[] data=null;
        try {
            String text = groupMsg.getText();
            String replace = text.replace("涩图", "");
            String replace1 = replace.replace("色图", "");
            String replace2 = replace1.replace(" ", "");
            randomHImage = PixivUtil.getImage(replace2);
            log.debug(randomHImage.toString());
            CatCodeUtil util = CatCodeUtil.INSTANCE;
            data = randomHImage.getData();
            String image = util.toCat("image", true, "url="+data[0].getUrls().getSmall());
            sender.sendGroupMsg(groupMsg, "画师ID:"+data[0].getUid()+"丨画师名:"+data[0].getAuthor()
                    +"|标题:"+data[0].getTitle()+"|画廊ID:"+data[0].getPid()+"\n"+"|图片地址:"+data[0].getUrls().getOriginal()+image);
        } catch (Exception e) {
            if(data == null ){
                sender.sendGroupMsg(groupMsg,"图片呗怪物吃掉了显示不出来惹");
                return;
            }else {
                sender.sendGroupMsg(groupMsg, "图片呗怪物吃掉了显示不出来惹:" + "画师ID:" + data[0].getUid() + "丨画师名:" + data[0].getAuthor()
                        + "|标题:" + data[0].getTitle() + "|画廊ID:" + data[0].getPid() + "\n" + "|图片地址:" + data[0].getUrls().getOriginal());
                return;
            }
        }
    }

    @OnGroup
    @Filters(value = {
            @Filter(value = "r18", matchType = MatchType.ENDS_WITH),
            @Filter(value = "R18", matchType = MatchType.ENDS_WITH)
    })
    public void GetHImg(GroupMsg groupMsg, Sender sender){
        PixivDTO randomHImage = null;
        ImgDTO[] data=null;
        try {
            String text = groupMsg.getText();
            String replace = text.replace("r18", "");
            String replace1 = replace.replace("R18", "");
            String replace2 = replace1.replace(" ", "");
            randomHImage = PixivUtil.getHImage(replace2);
            log.debug(randomHImage.toString());
            CatCodeUtil util = CatCodeUtil.INSTANCE;
            data = randomHImage.getData();
            String image = util.toCat("image", true, "url="+data[0].getUrls().getSmall());
            sender.sendGroupMsg(groupMsg, "画师ID:"+data[0].getUid()+"丨画师名:"+data[0].getAuthor()
                    +"|标题:"+data[0].getTitle()+"|画廊ID:"+data[0].getPid()+"\n"+"|图片地址:"+data[0].getUrls().getOriginal()+image);
        } catch (Exception e) {
            if(data == null ){
                sender.sendGroupMsg(groupMsg,"图片呗怪物吃掉了显示不出来惹");
                return;
            }else {
                sender.sendGroupMsg(groupMsg, "图片呗怪物吃掉了显示不出来惹:" + "画师ID:" + data[0].getUid() + "丨画师名:" + data[0].getAuthor()
                        + "|标题:" + data[0].getTitle() + "|画廊ID:" + data[0].getPid() + "\n" + "|图片地址:" + data[0].getUrls().getOriginal());
                return;
            }
        }
    }
}
